package io.renren.modules.areainfos.controller;

import java.util.HashMap;
import java.util.List;

import io.renren.common.annotation.SysLog;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.areainfos.entity.AreainfosEntity;
import io.renren.modules.areainfos.service.AreainfosService;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author 
 * @email 
 * @date 2018-10-30 17:39:18
 */
@RestController
@RequestMapping("/areainfos/areainfos")
public class AreainfosController {
	@Autowired
	private AreainfosService areainfosService;

	/**
	 * 查询地区列表
	 */
	@SysLog("查询全部地区列表")
	@RequestMapping("/list")
	@RequiresPermissions("areainfos:areainfos:list")
	public List list(){
		List<AreainfosEntity> areainfosList = areainfosService.queryList(new HashMap<>(16));
		return  areainfosList;
	}

	/**
	 * 根据父节点查询地区列表
	 */
	@SysLog("根据父节点查询地区列表")
	@PostMapping("/queryByParentId")
	@RequiresPermissions("areainfos:areainfos:list")
	public R queryByParentId(Integer columnid){
		if (columnid == null){
			columnid =0;
		}
		List<AreainfosEntity> areainfosList = areainfosService.queryByParentId(columnid);
		return  R.ok().put("areainfosList",areainfosList);
	}

	
	/**
	 * 信息
	 */
	@SysLog("根据ID查询地区信息")
	@RequestMapping("/info/{columnid}")
	@RequiresPermissions("areainfos:areainfos:info")
	public R info(@PathVariable("columnid") Integer columnid){
		AreainfosEntity areainfos = areainfosService.queryObject(columnid);
		return R.ok().put("areainfos", areainfos);
	}
	
	/**
	 * 保存
	 */
	@SysLog("新增地区信息")
	@RequestMapping("/save")
	@RequiresPermissions("areainfos:areainfos:save")
	public R save(@RequestBody AreainfosEntity areainfos){
		ValidatorUtils.validateEntity(areainfos, AddGroup.class);
		areainfosService.save(areainfos);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@SysLog("修改地区信息")
	@RequestMapping("/update")
	@RequiresPermissions("areainfos:areainfos:update")
	public R update(@RequestBody AreainfosEntity areainfos){
		ValidatorUtils.validateEntity(areainfos, UpdateGroup.class);
		areainfosService.update(areainfos);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除地区信息")
	@RequestMapping("/delete")
	@RequiresPermissions("areainfos:areainfos:delete")
	public R delete(@RequestBody Integer columnid){
		int count = areainfosService.isParentNode(columnid);
		if (count > 0){
			return R.error("请先删除此父节点下的元素");
		}
		areainfosService.delete(columnid);
		
		return R.ok();
	}
	
}
